package com.ssm.curd.tools;

import com.github.pagehelper.PageInfo;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Result
 * 统一返回封装
 * @author zjl
 * @date 2019/12/2
 */
public class Result {
    /**状态码*/
    private int code;
    /** 状态 **/
    private boolean flag;

    private String message;

    private Map<String,Object> extendData = new HashMap<>();

    public Result(){
        super();
    }

    public static Result success(){
        return success("操作成功");
    }
    public static Result success(String message){
        return new Result(true,message);
    }
    public static Result page(PageInfo pageInfo){
        return new Result(true,"操作成功").add("pageInfo",pageInfo);
    }

    public static Result error(){
        return error("操作失败");
    }

    public static Result error(String message){
        return new Result(false,message);
    }

    public Result(boolean result,String message){
        this.setResult(result);
        this.message = message;
    }

    public void setResult(boolean result) {
        if (result) {
            this.code = 1;
        } else {
            this.code = -1;
        }
        setFlag(result);
    }

    public Result add(String key, Object value) {
        this.extendData.put(key,value);
        return this;
    }

    public boolean isFlag() {
        return flag;
    }

    public void setFlag(boolean flag) {
        this.flag = flag;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Map<String,Object> getExtendData() {
        return extendData;
    }

}
